package util;

import java.util.List;

public class PageUtil<T> {
    //1.合理化用户需要访问的pageNo
    //2.将前端所需要的所有数据封装到pageUtil 中，传递到前端页面的时候只需要传递一个参数即可
    private Integer pageNo;//访问页
    private Integer pageSize;//每页展示数量
    private Integer rows;//总条数
    private Integer pageTotal;//总页数
    private List<T> list;//前端页面的数据

    //编写构造函数用来合法化pageNo,pageTotal
    public PageUtil(Integer pageNo, Integer pageSize, Integer rows) {
        //将pageSize，rows直接赋值
        this.pageSize = pageSize;
        this.rows = rows;
        //计算总页数
        this.pageTotal = this.rows % this.pageSize == 0 ?
                this.rows / this.pageSize : (this.rows / this.pageSize) + 1;
        if (this.pageTotal == 0) {
            this.pageTotal = 1;
        }
        //合法化pageNo
        if (pageNo == null) {
            this.pageNo = 1;
        } else if (pageNo < 1) {
            this.pageNo = 1;
        } else if (this.pageTotal == 0) {
            this.pageTotal = 1;
            this.pageNo = 1;
        } else if (pageNo > this.pageTotal) {
            this.pageNo = this.pageTotal;
        } else {
            this.pageNo = pageNo;
        }
    }


    public Integer getPageNo() {
        return pageNo;
    }


    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }


    public Integer getPageSize() {
        return pageSize;
    }


    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }


    public Integer getRows() {
        return rows;
    }


    public void setRows(Integer rows) {
        this.rows = rows;
    }


    public Integer getPageTotal() {
        return pageTotal;
    }


    public void setPageTotal(Integer pageTotal) {
        this.pageTotal = pageTotal;
    }


    public List<T> getList() {
        return list;
    }


    public void setList(List<T> list) {
        this.list = list;
    }


}
